package gui;

import entities.Student;
import entities.Manager;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import java.util.Iterator;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import containers.StudentContainer;

/**
 * 
 * @author Samuel Ferguson sff803
 * 
 */
public class ModifyInterestPanel extends JPanel {

	ModifyInterestPanel thisone;
	ValueEntryPanel enterInterest;
	int id;
	Object[][] data;

	public ModifyInterestPanel() {
		super(new GridLayout(2, 0));
		thisone = this;
		JButton button;
		NumberFormat format = NumberFormat.getInstance();
		String[] columnNames = { "id", "interest rate", "profit" };
		data = new Object[StudentContainer.dictionary().size()][columnNames.length];

		Student temp;
		int i = 0;
		Iterator<Student> iter = StudentContainer.dictionary().values()
				.iterator();
		while (iter.hasNext()) {
			temp = iter.next();
			if (temp.getLoan().getTotalGiven() > 0) {
				double interest = temp.getLoan().getInterest()
						+ Manager.interestRate;
				data[i][0] = temp.getID();
				data[i][1] = format.format(interest);
				data[i][2] = format.format(temp.getLoan().getTotalGiven()
						* interest * 48);
				i++;
			}
		}

		JTable table = new JTable(data, columnNames);
		table.setPreferredScrollableViewportSize(new Dimension(500, 70));
		table.setFillsViewportHeight(true);

		// Create the scroll pane and add the table to it.
		JScrollPane scrollPane = new JScrollPane(table);

		// Add the scroll pane to this panel.
		add(scrollPane);
		double interest = Manager.interestRate;
		String myString = "The base interest rate is "
				+ format.format(interest);
		add(new JLabel(myString));
		enterInterest = new ValueEntryPanel("New Interest Rate");
		add(enterInterest);

		JPanel okay = new JPanel();
		button = new JButton("Update Base Interest");
		button.addActionListener(new MakeEmployee());
		okay.add(Box.createVerticalGlue());
		okay.add(button);

		add(okay);

	}

	private class MakeEmployee implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent event) {

			float myInt = enterInterest.getValueAsFloat();

			if (myInt >= 0.0) {

				Manager.interestRate = myInt;

			}
			JPanel maAndOrPa = (JPanel) thisone.getParent();
			maAndOrPa.remove(thisone);
			maAndOrPa.add(new ModifyInterestPanel());
			maAndOrPa.revalidate();
		}
	}

	public static final long serialVersionUID = 1;

}
